#!/bin/bash
set -eo pipefail

defaultAlpineVersion='3.6'
declare -A alpineVersion=(
	[17.03]='3.5'
	[17.03-rc]='3.5'
	[17.05]='3.5'
)

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"

source '.architectures-lib'

versions=( "$@" )
if [ ${#versions[@]} -eq 0 ]; then
	versions=( */ )
fi
versions=( "${versions[@]%/}" )

# see http://stackoverflow.com/a/2705678/433558
sed_escape_lhs() {
	echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
}
sed_escape_rhs() {
	echo "$@" | sed -e 's/[\/&]/\\&/g' | sed -e ':a;N;$!ba;s/\n/\\n/g'
}

# "tac|tac" for http://stackoverflow.com/a/28879552/433558
dindLatest="$(curl -fsSL 'https://github.com/docker/docker/commits/master/hack/dind.atom' | tac|tac | awk -F '[[:space:]]*[<>/]+' '$2 == "id" && $3 ~ /Commit/ { print $4; exit }')"

# TODO once "Supported Docker versions" minimums at Docker 1.8+ (1.6 at time of this writing), bring this back again
#sed -r -e 's/^(ENV DIND_COMMIT) .*/\1 '"$dindLatest"'/' Dockerfile-dind.template

dockerVersions="$(
	{
		git ls-remote --tags https://github.com/docker/docker-ce.git

		# TODO remove-me (17.06+ live exclusively in docker-ce)
		git ls-remote --tags https://github.com/docker/docker.git
	} \
		| cut -d$'\t' -f2 \
		| grep '^refs/tags/v[0-9].*$' \
		| sed 's!^refs/tags/v!!; s!\^{}$!!' \
		| sort -ruV
)"

travisEnv=
for version in "${versions[@]}"; do
	rcGrepV='-v'
	rcVersion="${version%-rc}"
	if [ "$rcVersion" != "$version" ]; then
		rcGrepV=
	fi
	fullVersion="$(echo "$dockerVersions" | grep $rcGrepV -- '-rc' | grep "^$rcVersion[.]" | head -n1)"
	if [ -z "$fullVersion" ]; then
		echo >&2 "warning: cannot find full version for $version"
		continue
	fi

	channel='edge'
	if [ "$rcVersion" != "$version" ]; then
		channel='test'
	elif \
		minorVersion="${rcVersion##*.}" \
		&& minorVersion="${minorVersion#0}" \
		&& [ "$(( minorVersion % 3 ))" = '0' ] \
	; then
		channel='stable'
	fi

	archCase='apkArch="$(apk --print-arch)"; '$'\\\n'
	archCase+=$'\t''case "$apkArch" in '$'\\\n'
	for apkArch in $(apkArches "$version"); do
		dockerArch="$(apkToDockerArch "$version" "$apkArch")"
		archCase+=$'\t\t'"$apkArch) dockerArch='$dockerArch' ;; "$'\\\n'
	done
	archCase+=$'\t\t''*) echo >&2 "error: unsupported architecture ($apkArch)"; exit 1 ;;'$'\\\n'
	archCase+=$'\t''esac'

	alpine="${alpineVersion[$version]:-$defaultAlpineVersion}"

	sed -r \
		-e 's!%%DOCKER-CHANNEL%%!'"$channel"'!g' \
		-e 's!%%DOCKER-VERSION%%!'"$fullVersion"'!g' \
		-e 's!%%ALPINE-VERSION%%!'"$alpine"'!g' \
		-e 's!%%ARCH-CASE%%!'"$(sed_escape_rhs "$archCase")"'!g' \
		Dockerfile.template > "$version/Dockerfile"
	cp -a docker-entrypoint.sh "$version/"
	cp -a dockerd-entrypoint.sh "$version/dind/"

	for variant in git dind; do
		sed -r \
			-e 's!%%VERSION%%!'"$version"'!g' \
			"Dockerfile-$variant.template" > "$version/$variant/Dockerfile"
	done

	travisEnv='\n  - VERSION='"$version$travisEnv"
done

travis="$(awk -v 'RS=\n\n' '$1 == "env:" { $0 = "env:'"$travisEnv"'" } { printf "%s%s", $0, RS }' .travis.yml)"
echo "$travis" > .travis.yml
